package cn.nbxz.dao;

import cn.nbxz.domain.Goods;
import cn.nbxz.domain.Order;
import cn.nbxz.domain.OrderItem;
import cn.nbxz.util.JdbcHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderItemDao implements Dao<OrderItem,String> {
    private String table=" t_order_items ";
    @Override
    public OrderItem load(String id) {
        String SQL="select id,goods_id,price,goods_number,order_id from "+table+"where id=?";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , id ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            OrderItem o =   this.cast( map );
            return o ;
        }
        return null;
    }

    @Override
    public List<OrderItem> list() {
        String SQL="select id,goods_id,price,goods_number,order_id from "+table;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL ) ;
        helper.destory();
        if( list != null && list.size() >0) {
            List<OrderItem> orderItems = new ArrayList<>();
            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);
                OrderItem c =   this.cast( map );
                orderItems.add(c);
            }
            return orderItems;
        }

        return null;
    }

    @Override
    public OrderItem cast(Map<String, Object> map) {
        OrderItem orderItem=new OrderItem();
        orderItem.setId((String)map.get("id"));

        Goods goods= new GoodsDao().load((Integer)map.get("goods_id"));
        orderItem.setGoods(goods);

        orderItem.setPrice((double)map.get("price"));

        orderItem.setNumber((int)map.get("goods_number"));
        Order order=new OrderDao().load((String) map.get("order_id"));
        orderItem.setOrder(order);
        return orderItem;
    }

    @Override
    public boolean save(OrderItem o) {
        String SQL="insert into "+table+" (id,goods_id,price,goods_number,order_id) "+
                " values(?,?,?,?,?)";
       Integer goods_id= o.getGoods().getId();
      String order_id= o.getOrder().getId();
      Object[] array={o.getId(),goods_id,o.getPrice(),o.getNumber(),order_id};
      JdbcHelper helper = new JdbcHelper();
        int id= helper.insert( SQL , array );
        //OrderItem id?????
        helper.destory();
        return id != -1 ;
    }

    @Override
    public boolean update(OrderItem o) {
        String SQL="update "+table+"set goods_id=?, price=?, goods_number=?, order_id=?"
                +"where id=?";
        Integer goods_id= o.getGoods().getId();
        String order_id= o.getOrder().getId();
        Object[] array={goods_id,o.getPrice(),o.getNumber(),order_id,o.getId()};
        JdbcHelper helper = new JdbcHelper();
        int id=helper.update(SQL,array);
        helper.destory();
        return id != -1 ;
    }

    @Override
    public boolean delete(String id) {
        String SQL = "DELETE FROM " + table + " WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , id );
        helper.destory();
        return count == 1 ;
    }
}
